package com.letter.otools.day.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.letter.otools.day.R;


//键值对view
public class KeyValueView extends RelativeLayout {

    TextView tvKey;
    TextView requiredTv;
    TextView tvValue;
    ImageView ivRightImage;
    ImageView ivLeftImage;
    ImageView ivPreIcon;
    RelativeLayout layValue;
    RelativeLayout tvItem;
    ProgressBar loading;
    private View lineView;

    public enum ValueGravity {
        LEFT,
        RIGHT,
    }

    private float getFontWidth(Paint paint, String text) {
        return paint.measureText(text);
    }

    private int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public KeyValueView(Context context) {
        this(context, null);
    }

    public void setRequiredTv(int visibility) {
        this.requiredTv.setVisibility(visibility);
    }

    public KeyValueView(final Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.view_keyvalue, this);
        tvKey = (TextView) view.findViewById(R.id.tvKey);
        lineView = view.findViewById(R.id.lineView);
        requiredTv = view.findViewById(R.id.requiredTv);
        //TextView tvKeyLong = view.findViewById(R.id.tvKeyLong);
        tvValue = (TextView) view.findViewById(R.id.tvValue);
        ivRightImage = (ImageView) view.findViewById(R.id.ivRightImage);
        ivLeftImage = (ImageView) view.findViewById(R.id.ivLeftIcon);
        ivPreIcon = (ImageView) view.findViewById(R.id.ivPreIcon);
        layValue = view.findViewById(R.id.layValue);
        tvItem = view.findViewById(R.id.tvItem);
        loading = view.findViewById(R.id.loading);
        //tvKey = tvKeyShort;
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.keyValueView);
            String key = typedArray.getString(R.styleable.keyValueView_kvkey);
            String value = typedArray.getString(R.styleable.keyValueView_kvvalue);
            String valuehint = typedArray.getString(R.styleable.keyValueView_kvvaluehint);
            int keycolor = typedArray.getColor(R.styleable.keyValueView_kvkeyColor, getResources().getColor(R.color.textColorItemName));
            int valuecolor = typedArray.getColor(R.styleable.keyValueView_kvvalueColor, getResources().getColor(R.color.textColorItemName));
            int valuehintColor = typedArray.getColor(R.styleable.keyValueView_kvvaluehintColor, Color.parseColor("#989898"));
            int imageRight = typedArray.getResourceId(R.styleable.keyValueView_kvrightImage, -1);
            int imageLeft = typedArray.getResourceId(R.styleable.keyValueView_kvleftImage, -1);
            int textPreImage = typedArray.getResourceId(R.styleable.keyValueView_kvtextPreImage, -1);
            int valueGravity = typedArray.getInt(R.styleable.keyValueView_kvvalueGravity, 0);
            int itemPadding = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvitemPadding, -1);
            int kvitemPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvitemPaddingLeft, -1);
            int kvitemPaddingRight = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvitemPaddingRight, -1);
            int kvitemPaddingTop = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvitemPaddingTop, -1);
            int kvitemPaddingBottom = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvitemPaddingBottom, -1);
            int valueTextSize = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvvalueTextSize, -1);
            int keyTextSize = typedArray.getDimensionPixelSize(R.styleable.keyValueView_kvkeyTextSize, -1);
            boolean valueSingleLine = typedArray.getBoolean(R.styleable.keyValueView_kvvalueSingleLine, true);
            boolean kvrequired = typedArray.getBoolean(R.styleable.keyValueView_kvrequired, false);

            boolean kvkeyWrapWidth = typedArray.getBoolean(R.styleable.keyValueView_kvkeyWrapWidth, true);
            typedArray.recycle();

            boolean useLongText = kvkeyWrapWidth
                    || valueGravity == 1
                    || TextUtils.isEmpty(value)
                    || getFontWidth(tvKey.getPaint(), key) > dp2px(90);

            if (useLongText) {
//                tvKeyShort.setVisibility(View.GONE);
//                tvKeyLong.setVisibility(View.VISIBLE);
//                tvKey = tvKeyLong;
                setKeyWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                setKeyWidth(dp2px(90));
            }
            tvKey.setText(key);
            tvKey.setTextColor(keycolor);
//            tvValue.setEllipsize(TextUtils.TruncateAt.END);

            tvValue.setText(value);
            tvValue.setTextColor(valuecolor);
            tvValue.setHintTextColor(valuehintColor);
            tvValue.setSingleLine(valueSingleLine);
            if (!TextUtils.isEmpty(valuehint)) {
                tvValue.setHint(valuehint);
            }

            if (-1 != imageRight) {
                ivRightImage.setVisibility(VISIBLE);
                ivRightImage.setImageResource(imageRight);
            }
            if (-1 != imageLeft) {
                ivLeftImage.setVisibility(VISIBLE);
                ivLeftImage.setImageResource(imageLeft);
            }
            if (-1 != itemPadding) {
//                tvItem.setPadding(itemPadding, itemPadding, itemPadding, itemPadding);
                setItemPadding(itemPadding);
            } else if (-1 != kvitemPaddingLeft ||
                    -1 != kvitemPaddingTop ||
                    -1 != kvitemPaddingRight ||
                    -1 != kvitemPaddingBottom) {
                setItemPadding(kvitemPaddingLeft, kvitemPaddingTop, kvitemPaddingRight, kvitemPaddingBottom);
            }
            if (-1 != valueTextSize) {
                tvValue.getPaint().setTextSize(valueTextSize);
            }
            if (-1 != keyTextSize) {
                tvKey.getPaint().setTextSize(keyTextSize);
            }
            if (textPreImage != -1) {
                ivPreIcon.setVisibility(VISIBLE);
                ivPreIcon.setImageResource(textPreImage);
            }
            if (1 == valueGravity) {
                layValue.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                tvValue.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            }
            setRequiredTv(kvrequired ? VISIBLE : GONE);
        }

    }

    public void showLineView(int left) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lineView.getLayoutParams());
        lp.setMargins(left, 0, 0, 0);
        lineView.setLayoutParams(lp);
        lineView.setVisibility(VISIBLE);

    }

    public KeyValueView setKeyWidth(int width) {
        ViewGroup.LayoutParams params = tvKey.getLayoutParams();
        params.width = width;//ViewGroup.LayoutParams.WRAP_CONTENT;
        tvKey.setLayoutParams(params);
//        tvKey.setWidth(width);
        return this;
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public KeyValueView setValue(CharSequence value) {
        this.tvValue.setText(value);
        return this;
    }

    public KeyValueView setSingleLine(boolean isSingleLine) {
        tvValue.setSingleLine(isSingleLine);
        return this;
    }

    public String getValueText() {
        return tvValue.getText().toString();
    }

    public KeyValueView setTextPreIcon(int textPreIcon) {
        ivPreIcon.setVisibility(VISIBLE);
        ivPreIcon.setImageResource(textPreIcon);
        return this;
    }

    public void showLoading() {
        loading.setVisibility(VISIBLE);
        layValue.setVisibility(View.GONE);
    }

    public boolean isLoadingShow() {
        return loading.getVisibility() == VISIBLE;
    }

    public void hideLoading() {
        loading.setVisibility(GONE);
        layValue.setVisibility(View.VISIBLE);
    }


    public KeyValueView setKey(CharSequence key) {
        this.tvKey.setText(key);
        return this;
    }

    public KeyValueView setRightImageResource(Integer imageId) {
        if (imageId != null && imageId > 0) {
            ivRightImage.setVisibility(VISIBLE);
            ivRightImage.setImageResource(imageId);
        } else {
            ivRightImage.setVisibility(GONE);
        }

        return this;
    }

    public KeyValueView setRightImageVisibility(int visibility) {
        ivRightImage.setVisibility(visibility);
        return this;
    }

    public KeyValueView setPreIcon(int imageId) {
        if (imageId <= 0) {
            ivPreIcon.setVisibility(GONE);
        } else {
            ivPreIcon.setVisibility(VISIBLE);
            ivPreIcon.setImageResource(imageId);
        }

        return this;
    }

    public KeyValueView setValueTextSize(int valueTextSize) {
        return setValueTextSize(TypedValue.COMPLEX_UNIT_SP, valueTextSize);
    }

    public KeyValueView setValueTextSize(int unit, int valueTextSize) {
        tvValue.setTextSize(unit, valueTextSize);
        return this;
    }


    public KeyValueView setValueSingleLine(boolean singleLine) {
        tvValue.setSingleLine(singleLine);
        return this;
    }

    public KeyValueView setKeyTextSize(int keyTextSize) {
        return setKeyTextSize(TypedValue.COMPLEX_UNIT_SP, keyTextSize);
    }

    public KeyValueView setKeyTextSize(int unit, int keyTextSize) {
        tvKey.setTextSize(unit, keyTextSize);
        return this;
    }

    public KeyValueView setValueTextColor(int valuecolor) {
        tvValue.setTextColor(valuecolor);
        return this;
    }

    public KeyValueView setKeyTextColor(int keycolor) {
        tvKey.setTextColor(keycolor);
        return this;
    }

    public KeyValueView setItemPadding(int itemPadding) {
//        tvItem.setPadding(itemPadding, itemPadding, itemPadding, itemPadding);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tvItem.getLayoutParams();
        layoutParams.setMargins(itemPadding, itemPadding, itemPadding, itemPadding);
        return this;
    }

    public KeyValueView setItemPadding(int start, int top, int end, int bottom) {
//        tvItem.setPadding(start, top, end, bottom);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tvItem.getLayoutParams();
        layoutParams.setMargins(start, top, end, bottom);

        return this;
    }

    public KeyValueView setValueGravity(ValueGravity valueGravity) {
        if (ValueGravity.RIGHT == valueGravity) {
            layValue.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            tvValue.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            setKeyWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            layValue.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
            setKeyWidth(dp2px(90));
        }
        return this;
    }

    public void setLayValueClickListener(OnClickListener l) {
        layValue.setOnClickListener(l);
    }

    public TextView getKeyTextView() {
        return this.tvKey;
    }

    public String getValue() {
        return this.tvValue.getText().toString();
    }

    public TextView getTvValue() {
        return this.tvValue;
    }
}
